# list1 = [1, 2, 3, 4, 5]
# n = 2
# list2 = []
#
#
# def combination(a, self.b):
#     if len(a) < self.b:
#         return
#     else:
#         lis = []
#         lis.append(a[0])
#         n = self.a[0]
#         self.a.pop(0)
#
#         for i in range(len(a)):
#             m = self.a[i]
#             li = [n, m]
#             list2.append(li)
#
#         return combination(a, self.b)
#
#
# combination(list1, n)
# print(list2)


# def combination_k(s, k):
#     if k == 0: return ['']
#     subletters = []
#     for i in range(len(s)):
#         for letter in combination_k(s[i + 1:], k - 1):
#             subletters.append([s[i]+ letter])
#         return subletters
#
#
# print(combination_k(list1, 3))


# list1 = [1, 2, 3, 4, 5]
# n = 2
# list2 = []
#
#
# def combination(a, self.b):
#     '''
#     当n小于长度时就返回
#     :param self.a:
#     :param self.b:
#     :return:
#     '''
#     lis1 = []
#     if len(a) < self.b:
#         return
#     elif self.b == 1:
#         for i in self.a:
#             lis1.append(i)
#             list2.append(lis1)
#         return lis1.clear()
#     else:
#         while self.b > 1:
#             lis1.append(a[0])
#             self.a.pop(0)
#             self.b -= 1
#             return combination(a, self.b)
#         return combination(a, self.b)
#
#
# combination(list1, n)
# print(list2)

# TODO '数组组合'

class InPut:
    def __init__(self):
        self.list1 = []
        self.num1 = None

    def put(self):
        print("输入数组，已“a”结束")
        while 1:
            Int1 = input("：")
            if Int1 == 'a':
                break
            else:
                self.list1.append(int(Int1))
        self.num1 = int(input("输入组合个数："))
        n = input("去重请按:\t1\n")
        if int(n) == 1:
            self.delt(self.list1)

    # def __str__(self):
    #     return self.list1, self.num1
    def delt(self,lis):
        le = len(lis)
        for i in range(le):
            n = i + 1
            while n < le:
                if lis[i] == lis[n]:
                    lis.pop(n)
                    n -= 1
                    le -= 1
                n += 1


class Combination:
    def __init__(self, list1, n):
        self.a = list1
        self.b = n
        self.lis = []
        self.list2 = []

    def combination(self, c, a, b):
        d = a.copy()
        while b > 1:
            c.append(a[0])
            d.pop(0)
            b -= 1
            return self.combination(c, d, b)
        self.addLast(c, d, self.b)
        return
        # return self.combination(a, self.b)

    def addLast(self, c, a, b):
        for i in range(len(a)):
            li = c.copy()
            self.list2.append(li)
            self.list2[-1].append(a[i])
        c = []

    def prin(self):
        for i in range(len(self.list2)):
            if i % 5 == 0:
                print()
            print(self.list2[i], end='\t')

    def start(self):
        le = len(self.a)
        if le < self.b:
            return
        elif self.b == 1:
            self.addLast(self.lis, self.a, self.b)
            self.prin()
        else:
            if le >= self.b:
                for i in range((le - self.b) + 1):
                    li = [self.a[0]]
                    self.a.pop(0)
                    self.combination(li, self.a, (self.b - 1))
                    i -= 1
                    le -= 1
            self.prin()


if __name__ == '__main__':
    I = InPut()
    I.put()
    list1, n = I.list1, I.num1
    # list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    # n = 5
    a = Combination(list1, n)
    a.start()
